﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace FunctionScope.Models
{
    [DataContract(Name = "SinFunctionModel", Namespace = "wpfscope.codeplex.com")]
    public class SinFunctionModel : BaseFunctionModel
    {
        private double _phase;

        private double _frequency;

        private double _dcOffset;

        private double _amplitude;

        public SinFunctionModel() : base() { }

        public SinFunctionModel(SinFunctionModel orginal) : base(orginal)
        {
            Copy(orginal);
        }

        public override BaseFunctionModel Clone()
        {
            return new SinFunctionModel(this);
        }

        public override void Copy(BaseFunctionModel target)
        {
            SinFunctionModel s = target as SinFunctionModel;
            if (s != null)
            {
                base.Copy(target);
                Phase = s.Phase;
                Frequency = s.Frequency;
                DcOffset = s.DcOffset;
                Amplitude = s.Amplitude;
            }
            
        }

        public override double Sample(double t)
        {
            //Todo, can use complex phasor to rotate this per sample period, maybe if Math.Sin isn't quick enough.
            double phase = (Phase / 360.0d) * Math.PI * 2.0d;   //Degrees to rads
            double freq = Frequency * Math.PI * 2.0d;           //Angular frequency
            return Amplitude * Math.Sin(freq * t + phase) + DcOffset;
        }

        public override double Max
        {
            get
            {
                return DcOffset + Amplitude;
            }
        }

        public override double Min
        {
            get
            {
                return DcOffset - Amplitude;
            }
        }

        [DataMember]
        public double Phase
        {
            get
            {
                return _phase;
            }

            set
            {
                _phase = value;
                RaisePropertyChanged("Phase");
            }
        }

        [DataMember]
        public double Frequency
        {
            get
            {
                return _frequency;
            }

            set
            {
                _frequency = value;
                RaisePropertyChanged("Frequency");
            }
        }

        [DataMember]
        public double DcOffset
        {
            get
            {
                return _dcOffset;
            }

            set
            {
                _dcOffset = value;
                RaisePropertyChanged("DcOffset");
            }
        }

        [DataMember]
        public double Amplitude
        {
            get
            {
                return _amplitude;
            }

            set
            {
                _amplitude = value;
                RaisePropertyChanged("Amplitude");
            }
        }
    }
}
